<?php
class Zendvn_Model_Table_NewsItem extends Zend_Db_Table{
	protected $_name = "news_item";
	protected $_primary = "id"; 
	protected $_arrParam = array();
	
	public $_searchFields = array('name'=>'- Name field','code'=>'- Code field');
	
	protected $_listSQL;
	public function statusItem($arrParam = null,$options = null){
		if($options == null){
			$status = ($arrParam['s'] == 'active' )? 'inactive':'active';				
			$data = array('status'=>$status);
			$where = 'id = ' . (int)$arrParam['id'];
			$this->update($data, $where);	
		}
		
		if($options['task'] == 'multi'){
			
			$status = $arrParam['s'];				
			if(count($arrParam['cid'])>0){
				if($status == 'active'){
					$ids = implode(',', $arrParam['cid']);
					$where = 'id IN (' . $ids . ')';
					$data = array('status'=>$status);
					$this->update($data, $where);
				}else{
					foreach ($arrParam['cid'] as $val){						
						$where = 'id = ' . $val;
						$data = array('status'=>$status);
						$this->update($data, $where);						
					}
				}
			}
		}
	}
	
	public function deleteItem($arrParam = null,$options = null){
		$model = new Zendvn_Model_DeleteRecode();
		$model->newsItem($arrParam,$options,$this);
		
		$tblItem = new Zendvn_Model_Table_LinkContent();
		$where = 'fk_news_item = ' . (int)$arrParam['id'];
		$tblItem->delete($where);
	}
	public function getItem($arrParam = null,$options = null){
		
		if($options == null){
            $acl= new Zendvn_Acl();

            $info = new Zendvn_System_Info();
            $idUser = $info->getMemberInfo('id');

            $where = 'id = ' . (int)$arrParam['id'];
            if(!$acl->isAdminAccess()){
                $where.=' AND created_by='.$idUser;
            }
			$result = $this->fetchRow($where)->toArray();
		}
		if($options['task'] == 'info'){
			$db = $this->getAdapter();
			$select = $db->select()
						->from('news_item AS i')						
						->where('i.id = ?',$arrParam['id'],1)
						->joinLeft('users AS u1','u1.id = i.created_by', array ('u1.email AS created_by'))
						->joinLeft('users AS u2','u2.id = i.modified_by',array ('u2.email AS modified_by'));			
			$result = $db->fetchRow($select);

		}
        if(!empty($result['cat_id'])){
            $db = $this->getAdapter();
            $select = $db->select()
                         ->from('news_category AS c',array('name'))
                         ->where('c.id IN(?)',json_decode($result['cat_id'],true));
            $category = $db->fetchCol($select);
            $result['occupation'] = $category;
        }
		return $result;
	}
	
	public function listItem($arrParam = null,$options = null){

		if($options == null){
            $info = new Zendvn_System_Info();
            $idUser = $info->getMemberInfo('id');
			$db = $this->getAdapter();
			$select = $db->select()
						 ->from('news_item AS i')
						 ->joinLeft('users AS u1','u1.id = i.created_by', array ('u1.email AS created_by'))
						 ->joinLeft('users AS u2','u2.id = i.modified_by',array ('u2.email AS modified_by'));
			$arrFilter = $arrParam['arrFilter'];
			
			if(!empty($arrFilter['col'])){
				$select->order($arrFilter['col'] . ' ' . $arrFilter['order']);			
			}
	
			if(!empty($arrFilter['status']) && $arrFilter['status'] != 'none'){
				$select->where('i.status = ?',$arrFilter['status']);
			}
			
			if(!empty($arrFilter['cat_id']) && $arrFilter['cat_id'] != 1){
				$tblCate 		= new Zendvn_Model_Table_NewsCategory();
				$arrCid 		= $tblCate->getChildrenNode($arrFilter['cat_id'],array('node'=>true,'fields'=>array('id')));
				$select->where('i.cat_id IN (?)',$arrCid);
			}
			
			if(!empty($arrFilter['keyword']) && !empty($arrFilter['searchField'])){
				$keyword = '%' . $arrFilter['keyword'] . '%';
				$select->where($arrFilter['searchField'] . ' LIKE ? ', $keyword);
			}

            $acl= new Zendvn_Acl();
            if(!$acl->isAdminAccess()){
                $select->where('created_by = ?',$idUser);
            }
			$this->_listSQL = $select;
			$panigator = $arrParam['panigator'];
			$select->limitPage($panigator['page'], $panigator['rowCount']);
			
			$result = $db->fetchAll($select);

		}
		
		if($options['task'] == 'list-item'){
			$db = $this->getAdapter();
			$select = $db->select()
						 ->from('news_item AS i')
						 ->where('i.status = "active"');
						 
			switch(@$arrParam['order']){
				case 'name' :
						$select->order('i.full_name ASC');
						break;
				case 'popular' :
						$select->order('i.view DESC');
						break;
				case 'random' :
						$select->order('rand()');
						break;	
				case 'recent' :
						$select->order('i.recent DESC');
						break;	
				default :	
						$select->order('i.id DESC');
						break;	
			}

            if(!empty($arrParam['full_name'])){
                $keyword = '%' . $arrParam['full_name'] . '%';
                $select->where('i.full_name' . ' LIKE ? ', $keyword);
            }

            if(!empty($arrParam['search_full_name'])){
                $keyword = '%' . $arrParam['search_full_name'] . '%';
                $select->where('i.full_name' . ' LIKE ? ', $keyword);
            }
            
            if(!empty($arrParam['website'])){
                $keyword = '%' . $arrParam['website'] . '%';
                $select->where('i.website' . ' LIKE ? ', $keyword);
            }
            if(!empty($arrParam['country'])){
                $keyword = '%' . $arrParam['country'] . '%';
                $select->where('i.country' . ' LIKE ? ', $keyword);
            }
            if(!empty($arrParam['email'])){
                $keyword = '%' . $arrParam['email'] . '%';
                $select->where('i.email' . ' LIKE ? ', $keyword);
            }
            if(!empty($arrParam['city'])){
                $keyword = '%' . $arrParam['city'] . '%';
                $select->where('i.city' . ' LIKE ? ', $keyword);
            }

			$this->_listSQL = $select;
			$panigator = $arrParam['panigator'];
			$select->limitPage($panigator['page'], $panigator['rowCount']);
			$result = $db->fetchAll($select);	
		}
		if($options['task'] == 'list-archive'){
			$db = $this->getAdapter();
			$select = $db->select()
						 ->from('news_item AS i')
						 ->where('i.cat_id = 2')
						 ->where('i.id NOT IN (?)', $arrParam['arrId'])
						 ->where('i.status = "active"')
						 ->order('i.id DESC');
			$select->limitPage(0, 4);
			$result = $db->fetchAll($select);	
		}
		return $result;
	}
	
	public function totalItem($arrParam = null,$options = null){
		
		if($options == null){
			$db = $this->getAdapter();
			$select = $this->_listSQL->reset(Zend_Db_Select::COLUMNS);
			$select = $this->_listSQL->reset(Zend_Db_Select::LIMIT_COUNT);
			$select = $this->_listSQL->reset(Zend_Db_Select::LIMIT_OFFSET);
			$select = $this->_listSQL->reset(Zend_Db_Select::ORDER);
			$select = $this->_listSQL->reset(Zend_Db_Select::GROUP);
			$select->columns('COUNT(i.id) AS total');	
			$result = $db->fetchOne($select);
		}
		return $result;
		
	}
	
	public function saveItem($arrParam = null,$options = null){
		
		$function  = Zendvn_System_Function::call($options['task']);
		return $this->$function($arrParam);
	}
	
	public function adminAdd($arrParam = null){
		$info = new Zendvn_System_Info();
		$idUser = $info->getMemberInfo('id');
		
		$row = $this->fetchNew();
		$row->full_name			= $arrParam['full_name'];
		$row->website			= $arrParam['website'];
		$row->country			= $arrParam['country'];
		$row->email			    = $arrParam['email'];
		$row->city			    = $arrParam['city'];
		$row->status		    = ($arrParam['status']=='') ? 'inactive':$arrParam['status'];
		$row->created		    = date('Y-m-d H:i:s');
		$row->created_by	    = $idUser;
		$row->cat_id		    = Zend_Json::encode($arrParam['cat_id']);
		$row->save();

        return $row->id;
			
	}
	
	public function adminEdit($arrParam = null){
		$info 				= new Zendvn_System_Info();
		$idUser 			= $info->getMemberInfo('id');
		
		$where  			= 'id = ' . (int)$arrParam['id'];
		$row 				= $this->fetchRow($where);

        $row->full_name			= $arrParam['full_name'];
        $row->website			= $arrParam['website'];
        $row->country			= $arrParam['country'];
        $row->email			    = $arrParam['email'];
        $row->city			    = $arrParam['city'];
        $row->status		    = ($arrParam['status']=='') ? 'inactive':$arrParam['status'];
        $row->modified		    = date('Y-m-d H:i:s');
        $row->modified_by	    = $idUser;
        $row->cat_id		    = Zend_Json::encode($arrParam['cat_id']);
		$row->save();
        return (int)$arrParam['id'];
	}
}